package com.ruoyi.web.controller.exa;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.exa.domain.*;
import com.ruoyi.exa.service.*;
import com.ruoyi.system.domain.BaseColumn;
import com.ruoyi.web.controller.api.vo.ExaBitchParPerResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试卷管理Controller
 * 
 * @author linziheng
 * @date 2025-06-13
 */
@RestController
@RequestMapping("/exa/parper")
@Api("试卷管理Controller")
public class ExaParperController extends BaseController
{
    @Autowired
    private IExaParperService exaParperService;
    @Autowired
    private IExaParperClassService iExaParperClassService;
    @Autowired
    private IExaClassQuestionsService exaClassQuestionsService;
    @Autowired
    private IExaBitchUserService iExaBitchUserService;
    @Autowired
    private IExaBitchService exaBitchService;

    /**
     * 查询试卷管理列表
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExaParper exaParper)
    {
        startPage();
        List<ExaParper> list = exaParperService.selectExaParperList(exaParper);
        List<ExaBitchParPerResultVo> voList = new ArrayList<>();
        for (ExaParper parPer:list){
            ExaBitchParPerResultVo exaBitchParPerResultVo = new ExaBitchParPerResultVo();
            //试卷ID
            exaBitchParPerResultVo.setParperId(parPer.getId());
            //试卷名
            exaBitchParPerResultVo.setParperName(parPer.getParperName());
            //总分数
            exaBitchParPerResultVo.setScore(parPer.getParperScore());
            //题目数
            exaBitchParPerResultVo.setParperNum(parPer.getQuestionNum());

            //小题数量
            ExaParperClass exaParperClass = new  ExaParperClass();
            exaParperClass.setParperId(parPer.getId());
            List<ExaParperClass> exaParperClassList = iExaParperClassService.selectExaParperClassList(exaParperClass);

            //单选
            List<ExaParperClass> collectSingle = exaParperClassList.stream().filter(p -> p.getType().equals("1")).collect(Collectors.toList());
            if(!collectSingle.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectSingle.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setSingleChoice(questions.size());
            }

            //多选
            List<ExaParperClass> collectMultiple = exaParperClassList.stream().filter(p -> p.getType().equals("2")).collect(Collectors.toList());
            if(!collectMultiple.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectMultiple.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setMultipleChoice(questions.size());
            }
            //判断
            List<ExaParperClass> collectJudge = exaParperClassList.stream().filter(p -> p.getType().equals("3")).collect(Collectors.toList());
            if(!collectJudge.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectJudge.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setJudge(questions.size());
            }
            //简答
            List<ExaParperClass> collectShortAnswer = exaParperClassList.stream().filter(p -> p.getType().equals("4")).collect(Collectors.toList());
            if(!collectShortAnswer.isEmpty()){
                ExaClassQuestions exaClassQuestions = new ExaClassQuestions();
                exaClassQuestions.setClassId(collectShortAnswer.get(0).getId());
                List<ExaClassQuestions> questions = exaClassQuestionsService.selectExaClassQuestionsList(exaClassQuestions);
                exaBitchParPerResultVo.setShortAnswer(questions.size());
            }


            //创建时间
            exaBitchParPerResultVo.setCreateTime(parPer.getCreateTime());
            //状态
            ExaBitch exaBitch = new ExaBitch();
            exaBitch.setParperId(parPer.getId());
            exaBitch.setStatus("2");
            List<ExaBitch> exaBitchList = exaBitchService.selectExaBitchList(exaBitch);
            if(exaBitchList.size()>0){
                exaBitchParPerResultVo.setStatus("1");
            }else {
                exaBitchParPerResultVo.setStatus("0");
            }
            voList.add(exaBitchParPerResultVo);
        }
        return getDataTable(voList);
    }


    /**
     * 客户端查询试卷管理列表
     */
    @ApiOperation("客户端查询试卷管理列表")
    @GetMapping("/listClient")
    public TableDataInfo listClient(BaseColumn baseColumn)
    {
        String[] split = baseColumn.getParperId().split(",");
        Long[] id = new Long[split.length];
        for (int i = 0; i < split.length; i++) {
            id[i] = Long.valueOf(split[i]);
        }
        startPage();
        List<ExaParper> list = exaParperService.selectExaParperByIds(id);
        return getDataTable(list);
    }

    /**
     * 导出试卷管理列表
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:export')")
    @Log(title = "试卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExaParper exaParper)
    {
        List<ExaParper> list = exaParperService.selectExaParperList(exaParper);
        ExcelUtil<ExaParper> util = new ExcelUtil<ExaParper>(ExaParper.class);
        util.exportExcel(response, list, "试卷管理数据");
    }

    /**
     * 获取试卷管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(exaParperService.selectExaParperById(id));
    }

    /**
     * 新增试卷管理
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:add')")
    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExaParper exaParper)
    {
        return toAjax(exaParperService.insertExaParper(exaParper));
    }

    /**
     * 复制试卷管理
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:addCopy')")
    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
    @GetMapping("/addCopy")
    public AjaxResult addCopy(Long exaParperId)
    {
        ExaParper exaParper = exaParperService.selectExaParperById(exaParperId);
        ExaParper newExaParper = new ExaParper();
        BeanUtils.copyProperties(newExaParper,exaParper);
        newExaParper.setCreateTime(DateUtils.getNowDate());
        return toAjax(exaParperService.insertExaParper(newExaParper));
    }

    /**
     * 修改试卷管理
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:edit')")
    @Log(title = "试卷管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExaParper exaParper)
    {
        return toAjax(exaParperService.updateExaParper(exaParper));
    }

    /**
     * 删除试卷管理
     */
    @PreAuthorize("@ss.hasPermi('exa:parper:remove')")
    @Log(title = "试卷管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(exaParperService.deleteExaParperByIds(ids));
    }
}
